package eletjatek;

import java.awt.*;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ZoZo
 */
public class GameOfLife extends JFrame{

    private JFrame menuFrame;
    private JMenuBar menu;
    private JMenu file;
    private JMenuItem start;
    private JMenuItem stop;
    private JMenuItem save;
    private JMenuItem load; 
    private JMenuItem exit;
    private JMenu help;
    private JMenuItem uManual;
    public PaintPanel tableP;
    private JPanel progressBar;
    public JLabel running;
    private JDialog fChooserD = new JDialog();
    private JFileChooser fChooser = new JFileChooser();
    public int x,y;

    GameOfLife(int x, int y, JFrame menuJFrame){
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);       //kilépés gombra ne csináljon semmit
        this.setTitle("Game of Life");
        this.setMinimumSize(new Dimension(x*10+18,y*10+88));
        this.setMaximumSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width-1,Toolkit.getDefaultToolkit().getScreenSize().height-150));
        this.setLocationRelativeTo(null);
        
        menuFrame=menuJFrame;
        this.x=x;
        this.y=y;

        menu=new JMenuBar();
        file=new JMenu("File");
        menu.add(file);
        start=new JMenuItem("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });
        file.add(start);
        stop=new JMenuItem("Stop");
        stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopActionPerformed(evt);
            }
        });
        file.add(stop);
        save=new JMenuItem("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        file.add(save);
        load=new JMenuItem("Load");
        load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadActionPerformed(evt);
            }
        });
        file.add(load);
        exit=new JMenuItem("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        file.add(exit);
        help=new JMenu("Help");
        menu.add(help);
        uManual=new JMenuItem("UserManual");
        uManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uManualActionPerformed(evt);
            }
        });
        help.add(uManual);
        this.setJMenuBar(menu);

        tableP=new PaintPanel(x,y,this);
        this.add(tableP);
        progressBar=new JPanel();
        this.add(progressBar,BorderLayout.SOUTH);
        running=new JLabel("Stop");
        progressBar.add(running);

        this.addComponentListener(new java.awt.event.ComponentAdapter(){
            public void componentResized(ComponentEvent e){
                tableP.formResized();
            }
        });
    }


    public void startActionPerformed(java.awt.event.ActionEvent evt) {
        tableP.setRunable(true);
        running.setText("Running");
    }

    private void stopActionPerformed(java.awt.event.ActionEvent evt) {
        tableP.setRunable(false);
        running.setText("Stop");
    }

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {
        tableP.save();
    }

    private void loadActionPerformed(java.awt.event.ActionEvent evt) {
        tableP.load();
    }

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        menuFrame.setVisible(true);
        this.dispose();
    }

    private void uManualActionPerformed(java.awt.event.ActionEvent evt) {
        File f=new File(System.getProperty("user.dir")+"/src/eletjatek/files/UserManual.pdf");
        try {
            Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + f.getAbsolutePath());
        } catch (IOException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
            Fault error=new Fault("Hibás vagy megszakított I/O művelet!");
            error.setVisible(true);
        }
    }
    
}